package com.zxl.yjb.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zxl.yjb.GlideApp;
import com.zxl.yjb.R;
import com.zxl.yjb.bean.UserBean;

import java.util.ArrayList;
import java.util.List;

public class MyFriendsListAdapter extends BaseQuickAdapter<UserBean, BaseViewHolder> implements View.OnClickListener {


    private final String type;
    private boolean showSelecteView;
    private List<String> userIdList = new ArrayList<>();
    private String origin = "";

    public MyFriendsListAdapter(int layoutResId, String type) {
        super(layoutResId, null);
        this.type = type;
    }

    public MyFriendsListAdapter(int layoutResId, String type,String origin) {
        super(layoutResId, null);
        this.type = type;
        this.origin = origin;
    }


    @Override
    protected void convert(BaseViewHolder holder, UserBean item) {
//        helper.setText(R.id.tv_user_name, item.userName);
//        helper.setText(R.id.tv_comment_content, item.content);
//        RatingBar rbStart = helper.getView(R.id.rb_star);
//        rbStart.setRating(item.star);
        if ("black_list".equals(type)) {//黑名单
            holder.setVisible(R.id.btn_delete, true)
                    .setVisible(R.id.iv_join, false)
                    .addOnClickListener(R.id.btn_delete);
        } else if (showSelecteView) {     //选择好友
            ImageView iv = holder.getView(R.id.iv_selected);
            LinearLayout ll_bg = holder.getView(R.id.ll_bg);
            String userId = item.getUid() + "";
            iv.setTag(userId);
            ll_bg.setTag(userId);
            holder.setVisible(R.id.iv_selected, true);
            iv.setOnClickListener(this);
            ll_bg.setOnClickListener(this);
            if (userIdList.contains(userId)) {
                holder.setImageResource(R.id.iv_selected, R.mipmap.login_f_pressed);
            } else {
                holder.setImageResource(R.id.iv_selected, R.mipmap.login_f_normal);
            }

        }else {
            holder.setVisible(R.id.iv_selected, false);//只是好友列表
        }


        holder.setText(R.id.tv_user_name, item.getNickname())
                .setText(R.id.tv_gender, item.getSex() == 0 ? "未知" : item.getSex() == 1 ? "男" : "女");
//                .setText(R.id.tv_age,item.ge());
        if (!TextUtils.isEmpty(origin)&&"nearby".equals(origin))
        {
            holder.setText(R.id.tv_gender, "好评度 "+item.getEvaluate()+"分");
        }
        GlideApp.with(mContext).load(item.getFace_img()).into((ImageView) holder.getView(R.id.iv_user_header));

        holder.addOnClickListener(R.id.iv_user_header);

    }


    public void setShowSelecteView(boolean show) {
        showSelecteView = show;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_selected||view.getId() == R.id.ll_bg) {
            String userId = (String) view.getTag();
            if (userIdList.contains(userId)) {//如果有了就就是二次点击，删除
                userIdList.remove(userId);
                if (view instanceof ImageView)
                {
                    ((ImageView) view).setImageResource(R.mipmap.login_f_normal);
                }
                else {
                    ImageView iv = view.findViewById(R.id.iv_selected);
                    if (iv!=null)
                    {
                        iv.setImageResource(R.mipmap.login_f_normal);
                    }
                }
            } else {
                userIdList.add(userId);
                if (view instanceof ImageView)
                {
                    ((ImageView) view).setImageResource(R.mipmap.login_f_pressed);
                }
                else {
                    ImageView iv = view.findViewById(R.id.iv_selected);
                    if (iv!=null)
                    {
                        iv.setImageResource(R.mipmap.login_f_pressed);
                    }
                }
            }
            notifyDataSetChanged();
        }
    }

    public List<String> getUserIdList() {
        return userIdList;
    }


}
